/**
 * @file   dc_captura.h
 * @author Pedro
 * @date   Thu Nov  6 12:23:38 2008
 * 
 * @brief  Funciones del captura de vídeo a través del Firewire
 * 
 * 
 */
#ifndef _DC_CAPTURA_H_
#define _DC_CAPTURA_H_

#include <stdio.h>
#include <stdlib.h>

#include "cv.h"
#include "highgui.h"

#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <unistd.h>
#define _LINUX_TIME_H
#define _DEVICE_H_
#include <linux/videodev.h>

#include <libraw1394/raw1394.h>
#include <libdc1394/dc1394_control.h>
#include "affine.h"

#include "dc1394_multiview.h"


#define NUM_VERSION "0.1"
#define MAX_CAMERAS 8   /* Max number of cameras allowed */
#define MAX_PORTS 4     /* Max number of cards allowed */
#define MAX_RESETS 5    /* Max number of bus reset to find a camera */
#define MAX_SYNC_TRY 10 /* Max number of DMA buffer dicards to synchronize two cams */


#ifndef LONG_MAX
#define LONG_MAX 999999 /* Max length in frames of a video */
#endif
#define HIGH_ACTIVE_INPUT 1
#define ON_TRIGGER_MODE 1
#define OFF_TRIGGER_MODE 0
#define TRIGGER_MODE_LEVEL 1
#define TRIGGER_MODE_PROGRAMMABLE 0

#define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define BN 1
#define COLOR 3

#define MAX_RESOLUTION_WIDTH 1392
#define MAX_RESOLUTION_HEIGHT 1040
#define OFFSET_WIDTH 0
#define OFFSET_HEIGHT 0


/* typedef enum */
/* { */
/*    NOT_PRESENT = 0, */
/*    MANUAL, */
/*    AUTO, */
/*    MANUAL_AUTO */
/* } feature_available_t; */

/* typedef struct */
/* { */
/*   feature_available_t available_modes; */
/*   feature_available_t working_mode; */
/*   unsigned int min,max; */
/*   unsigned int value; */
/* }feature_t; */



typedef struct
{
    /*********    Camera Members   **********/
    raw1394handle_t handle;
    dc1394_cameracapture camera;
    IplImage *image_tmp; 
    IplImage *image;
    int numCams;      
    u_int64_t g_guid; 
    quadlet_t UID[2];
    char vendor[50];
    char model[50];
    int written_frame;
    int frame_size;
    int feature_headers_size;

    /*********    Configuration Members   **********/
    int drop_frames;
    int num_dma_buffers;
    unsigned int width;
    unsigned int height;
    unsigned int offset_width;
    unsigned int offset_height;
    unsigned int mode;
    unsigned int format;
    unsigned int color_format;
    unsigned int channel;
    unsigned int speed;

    dc1394_feature_set feature_set;

    int frame_rate;
    int packet_size;
    char *dc_dev_name;
    int soft_resync;
    int ext_trigger;
    int color;
    double shutter_value;
    double shutter_value_new;
    int shutter_control;
}camera_control;

/* Quitar en cuanto se pueda la variable global */

void default_options(camera_control *);

int dc_init(camera_control *);
int dc_config(camera_control *, int width, int height, int xoffsec, int yoffset);
int dc_start_iso(camera_control *);
void dc_cameras_resync(camera_control *, int, int);
int dc_capture(camera_control *, int);
int dc_init_images(camera_control *cameras, int camNum);

int dc_get_features(camera_control *, int );
int dc_set_feature(camera_control *, int , int , int );
int dc_set_features(camera_control *, int );
void dc_print_features(camera_control *, int );
void dc_print_single_feature(dc1394_feature_info *);
int dc_check_feature_limits(dc1394_feature_info *, int );
int dc_trigger_external_mode_on (camera_control *cameras, int camNum);
int dc_trigger_external_mode_off (camera_control *cameras, int camNum);


int dc_auto_on_off_feature(camera_control *, int , int ,int );
int dc_on_off_feature(camera_control *, int , int ,int);
void dc_show_image(camera_control *, int);

void get_options(int argc,char *argv[], camera_control *cameras);


#endif
